package com.bwie.system.service.impl;

import com.bwie.common.domain.User;
import com.bwie.common.domain.request.UserReq;
import com.bwie.system.mapper.UserMapper;
import com.bwie.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @className: UserServiceImpl
 * @Description: TODO
 * @version: v1.８.0
 * @author:
 * @date:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByPhoneAndPassword(UserReq userReq) {

        return userMapper.findByPhoneAndPassword(userReq);
    }

    @Override
    public void updBalanceById(UserReq userReq) {
        BigDecimal balance = userReq.getBalance();
        UserReq req = new UserReq();
        req.setId(userReq.getId());
        User byPhoneAndPassword = findByPhoneAndPassword(req);
        BigDecimal bigDecimal = byPhoneAndPassword.getBalance();
        String sort = userReq.getSort();
        //扣款
        if(sort.equals("0")){
            //将金额变为负数
            BigDecimal negate = balance.negate();
            BigDecimal add = bigDecimal.add(negate);
            userReq.setBalance(add);
        }else{
            BigDecimal add = bigDecimal.add(balance);
            userReq.setBalance(add);
        }
        Integer i = userMapper.updBalanceById(userReq);

    }
}
